package com.zjpavt.client.page;

import com.zjpavt.client.Connection;
import com.zjpavt.client.hal.LedScreen;
import com.zjpavt.client.page.pageunit.ScreenLine;
import com.zjpavt.client.hal.network.NetworkManager;
import com.zjpavt.client.page.abstractpage.AbstractEditablePage;
import com.zjpavt.client.page.editableunit.EditUnitString;

import java.util.ArrayList;
import java.util.List;

/**
 * 第十一页
 * 网络模式页面
 * @author: gxy
 */
public class NetworkModelPage extends AbstractEditablePage {
    /**
     * 这里必须使用静态的 ，由于父类的进行初始化时用到了改参数但是子类没有初始化会报错
     * */
//	private static final NetworkManager NETWORK_MANAGER = NetworkManager.getInstance();
    private Connection connection = null;
    public NetworkModelPage(int pageNum, long mRefreshPeriod) {
        super(pageNum, mRefreshPeriod);
    }

    /**
     * 刷新页面 todo 自动刷新获取变化的信号值
     */
    @Override
    public void refreshPage() {
        LedScreen.getInstance().setText(getPageContent(null, NetworkManager.getInstance().getNetStatus()));
    }

    @Override
    public void initEditUnits() {
        addEditUnit(new EditUnitString(2, 1, 5, NetworkManager.getInstance().getStatusArray()));
    }

    /**
     *
     * @param netModel 网络模式 联通移动电信
     * @param netStatus 网络状态 4G NETWORK_ENTE NETWORK_AUTO
     * @return
     */
    private List<ScreenLine> getPageContent(String netModel, String netStatus) {
        if (connection == null) {
            connection = Connection.getInstance();
        }
        List<ScreenLine> result = new ArrayList<>();
        result.add(new ScreenLine.ScreenLineBuilder().text("Net mode ").pageNum(getPageNum()).build());
        String netStatusString = netStatus;
        if (NetworkManager.NETWORK_AUTO.equals(netStatus)) {
            if (NetworkManager.INTERFACE_ETH_0.equals(NetworkManager.getInstance().getActiveNetworkName())) {
                netStatusString = netStatusString + "_" + NetworkManager.NETWORK_ENTE;
            } else if (NetworkManager.INTERFACE_PPP_0.equals(NetworkManager.getInstance().getActiveNetworkName())) {
                netStatusString = netStatusString + "_" + NetworkManager.NETWORK_4G;
            }
        }
        result.add(new ScreenLine.ScreenLineBuilder().text(netStatusString).build());
        String receiveTime;
        String reconnectNum;
//		组装4G工作状态
        if (connection.getLastReceiveDataDateFrom4G() != 0 && NetworkManager.getInstance().is4G_Modal()) {
            receiveTime = String.format("%05d", (System.currentTimeMillis() - connection.getLastReceiveDataDateFrom4G())/1000);
            if (receiveTime.length() > 5) {
                receiveTime = receiveTime.substring(receiveTime.length() - 5);
            }
        } else {
            receiveTime = String.format("%05d",0);
        }
        reconnectNum = String.format("%2d", connection.get_4GReconnectNum() % 100);
        String _4GLine = NetworkManager.NETWORK_4G + "  :" + receiveTime + "   " + reconnectNum;
        result.add(new ScreenLine.ScreenLineBuilder().text(_4GLine).build());
        //		组装ente工作状态
        if (connection.getLastReceiveDataDateFromEnte() != 0 && !NetworkManager.getInstance().is4G_Modal()) {
            receiveTime = String.format("%05d", (System.currentTimeMillis() - connection.getLastReceiveDataDateFromEnte())/ 1000);
            if (receiveTime.length() > 5) {
                receiveTime = receiveTime.substring(receiveTime.length() - 5);
            }
            reconnectNum = String.format("%2d", connection.getEnteReconnectNum() % 100);
        } else {
            receiveTime = String.format("%05d",0);
        }
        reconnectNum = String.format("%2d", connection.getEnteReconnectNum() % 100);
        String enteLine =  NetworkManager.NETWORK_ENTE+ ":" + receiveTime + "   " + reconnectNum;
        result.add(new ScreenLine.ScreenLineBuilder().text(enteLine).build());
        return result;
    }

    public void onKeySet() {
        if (!super.isInEditMode()) {
            clearEditUnit();//清空编辑单元
            initEditUnits();//填充可编辑单元
        }
        super.onKeySet();
    }

    @Override
    public void saveVal() {
        String changeNetwork = getEditUnit(0).getText();
        NetworkManager.getInstance().setNetStatus(changeNetwork);
//		本处应该使用异步 实际过程总发现使用异步导致线程无法启动 不再使用线程池
        new Thread(()-> {
            NetworkManager.getInstance().setActiveNetwork(changeNetwork);
        }).start();
    }

    @Override
    public void setDefaultVal() {
        getEditUnit(0).setCurrentVal(NetworkManager.getInstance().getStatusStringsIndex());
    }
}
